package com.itheima.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.common.PageResults;



import com.itheima.common.R;

import com.itheima.dto.Roles;
import com.itheima.dto.UserDto;

import com.itheima.mapper.PermissionGroupMapper;
import com.itheima.mapper.UserMapper;

import com.itheima.pojo.User;
import com.itheima.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionGroupMapper permissionGroupMapper;


    @Autowired
    private HttpServletRequest request;


    //用户登录
    @Override
    public R login(String username, String password) {

        //将页面提交的密码password进行md5加密处理
        //String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(User::getEmail, username);
        User user = userMapper.selectOne(wrapper);

        if (user == null) {


            System.out.println("用户不存在");
            return R.error("用户不存在");

        }

        System.out.println("user.getPassword()" + user.getPassword());
        System.out.println("md5Password" + password);
        if (!user.getPassword().equals(password)) {
            System.out.println("密码错误");
            return R.error("密码错误");
        }
        if (user.getStatus() != 0) {
            System.out.println("该员工已禁用");
            return R.error("该员工已禁用");
        }
        return R.success(user);
    }

    //用户注册
    @Override
    public void register(String username ,String password) {
        //根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail,username);
        User user = userMapper.selectOne(wrapper);
        if (user==null){

            User user1 = new User();
            user1.setEmail(username);
            //String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
            user1.setPassword(password);
            user1.setSex("男");
            //在数据库中添加
            userMapper.insert(user1);
        }
    }

    @Override
    public void password(String oldPassword, String newPassword) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(User::getPassword,oldPassword);
        User user1= userMapper.selectOne(wrapper);

        LambdaUpdateWrapper<User> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.eq(User::getEmail,user1.getEmail());
        userMapper.update(user1,wrapper1);


    }

    //用户资料
    @Override
    public UserDto profile(Integer id) {
        User user = userMapper.selectById(id);

        UserDto userDto = new UserDto();
        userDto.setName(user.getUsername());
        List<String> role = new ArrayList<>();
        role.add(user.getRole());
        userDto.setRole(role);
        List<String> menus = new ArrayList<>();
        menus.add("123");
        menus.add("aaaa");
        Roles roles = new Roles(menus, new ArrayList<>());
        userDto.setRoles(roles);

        return userDto;


    }



    //用户列表
    @Override
    public PageResults page(Integer page, Integer pagesize, String username) {

        Page<User> p = new Page<>(page, pagesize);

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getUsername, username);
        Page<User> userPage = userMapper.selectPage(p, wrapper);


        PageResults pageResults = new PageResults();

        pageResults.setCounts((int) userPage.getTotal());
        pageResults.setPage(page);
        pageResults.setPagesize(pagesize);
        pageResults.setPages(((int) userPage.getTotal() / pagesize) + 1);
        List<User> records = userPage.getRecords();


        pageResults.setList(records);


        return pageResults;
    }

    //    用户删除
    @Override

    public void delete(Integer id) {


        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId, id);
        userMapper.delete(wrapper);
    }

    //添加
    @Override
    public int add(User user) {

        System.out.println(user);
        user.setSex("男");

        int insert = userMapper.insert(user);

        return insert;
    }

    //修改
    @Override

    public String modifyById( User user) {
        int i = userMapper.updateById(user);
        if(i>0){
            return "修改成功";
        }
        return "修改失败";

    }

    //用户简单列表
    @Override
    public List selectList(User user) {
        String username = user.getEmail();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail,username);
        User user1 = userMapper.selectOne(wrapper);
        ArrayList<User> users = new ArrayList<>();
        users.add(user1);
        return users;
    }




    //用户屏蔽、启用
    @Override
    public boolean disabled(Integer id) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(User::getId,id);
        User user = userMapper.selectOne(wrapper);
        if (user.getStatus()==null){
            return false;
        }
        if (user.getStatus()==0){
            user.setStatus(1);
        }
        user.setStatus(0);
        return true;
    }


    //用户详情(回显)
    @Override
    public User get(Integer id) {
        User user = userMapper.selectById(id);

        /*PermissionGroup permissionGroup = permissionGroupMapper.selectById(user.getId());

        UserPGDto userPGDto = new UserPGDto();
        BeanUtils.copyProperties(user,userPGDto);
        userPGDto.setPermissionGroupTitle(permissionGroup.getName());*/
        return user;
    }

    @Override
    public void reset(Integer id, String password) {

        User user = userMapper.selectById(id);
        user.setPassword(password);
    }


}

